package com.tiyende.kash.loop.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.KeyEvent
import android.view.WindowManager
import android.widget.LinearLayout
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.databinding.TiyendeDialogLoadingBinding
import com.tiyende.kash.loop.utils.TiyendeImageUtil

class TiyendeLoading(ctx: Context, isTouchOut: Boolean = false, isCancel: Boolean = false, msg: String = "") : Dialog(ctx, R.style.DialogTheme) {

    var binding: TiyendeDialogLoadingBinding = TiyendeDialogLoadingBinding.inflate(layoutInflater)

    /**
     * 点击系统返回按钮是否消失
     */
    private val isCancel: Boolean

    /**
     * the LoadDialog constructor
     * @param ctx        Context
     * @param isTouchOut boolean : true-->返回不消失，false-->按返回dismiss
     */
    init {
        //        this.getContext().setTheme(android.R.style.Theme_InputMethod);
        setContentView(binding.root)
        TiyendeImageUtil.instance?.loadImageGifRes(ctx, binding.gifLoading, R.mipmap.tiyende_loading)
        /**
         * 加载中文字
         */
//        if (msg.isEmpty()) {
//            binding.textLoading.visibility = View.GONE
//        } else {
//            binding.textLoading.visibility = View.VISIBLE
//            binding.textLoading.text = msg
//        }

        /* 点击返回按钮是否dismiss */
        this.isCancel = isCancel
        /* 点击外部是否dismiss */
        setCanceledOnTouchOutside(isTouchOut)
        val window = window
        val attributesParams = window?.attributes
        attributesParams?.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
        attributesParams?.dimAmount = 0.5f //背景透明度
//        attributesParams.dimAmount = 0f;//背景透明度
        window?.setAttributes(attributesParams)
        window?.setBackgroundDrawableResource(R.color.transparent) // 一句话搞定
        window?.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isCancel) {
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object {
        /**
         * LoadDialog
         */
        private var loadDialog: TiyendeLoading? = null

        /**
         * show the mDialogTextView
         *
         * @param context
         */
        fun show(context: Context, msg: String) {
            showDialog(context, isTouchOut = false, isCancel = true, msg = msg)
        }

        /**
         * show the mDialogTextView
         *
         * @param context  Context
         * @param isCancel boolean
         */
        fun show(context: Context, isTouchOut: Boolean, isCancel: Boolean, msg: String) {
            showDialog(context, isTouchOut, isCancel, msg)
        }

        /**
         * show the mDialogTextView
         *
         * @param context    Context
         * @param isTouchOut boolean, true is can't dimiss，false is can dimiss
         */
        private fun showDialog(context: Context, isTouchOut: Boolean, isCancel: Boolean, msg: String) {
            if (context is Activity) {
                if (context.isFinishing) {
                    return
                }
            }
            loadDialog?.let {
                if (it.isShowing) {
                    return
                }
            }
            loadDialog = TiyendeLoading(context, isTouchOut, isCancel, msg)
            loadDialog?.show()
        }

        /**
         * dismiss the mDialogTextView
         */
        fun dismiss(context: Context?) {
            try {
                if (context is Activity) {
                    if (context.isFinishing) {
                        loadDialog = null
                        return
                    }
                }
                loadDialog?.let {
                    if (it.isShowing){
                        val loadContext = it.context
                        if (loadContext is Activity) {
                            if (loadContext.isFinishing) {
                                loadDialog = null
                                return
                            }
                        }
                        it.dismiss()
                        loadDialog = null
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                loadDialog = null
            }
        }
    }
}
